% little script that will take you through selecting all the stuff you need
% to manually select in order to then automatically run the high res
% tracking script


%% make sure these parameters are appropriate!!
isReferenceSession = false;
localimagesize = [1720,1784];        %in pixels; important for loading the .raw files; should be in [width,height]
referenceimagesize = [1776,1800];
micronsPerPixel = 1/.449;
rootname = 'drift';
GcampSmoothSize = 80;       %smoothing kernel for  gcamp (in microns)
stimtype = 'moving';    %set this to 'moving' for moving stim; otherwise can leave empty for now

%% will be prompted to select stuff:
if isReferenceSession
    localimagesize = referenceimagesize;
    [fn,fd] = uigetfile('*.raw','select one .raw file in the experimental sequence');
    cd(fd);
    movdirectory = fd;
    mov = loadRawStack_v1(fn(1:10),referenceimagesize,1:100);
    avios = mov(:,:,2:2:end);
    avios = median(avios,3);
    disp('select vessel centers')
    % select vessel centers:
    [~,~,vesselMasks] = selectVesselCenters(avios,[],'gray');
    % select window mask for reference image
    disp('select window mask')
    figure;imagesc(avios);colormap('gray');axis equal
    roi = drawpolygon;refim_windowmask = createMask(roi);
    windowmask = refim_windowmask;
    selpath = uigetdir(cd,'Select directory where to save reference image data');
    cd(selpath);
    save('vesselmaskdata.mat','refim_windowmask','avios','vesselMasks','referenceimagesize','micronsPerPixel')
    vesselmaskname = [cd,'\vesselmaskdata.mat'];
else
    [fn,fd] = uigetfile('*.mat','select reference session data');
    vesselmaskname = [fd,fn];
    [fn,fd] = uigetfile('*.raw','select one .raw file in the sequence');
    cd(fd);
    mov = loadRawStack_v1(fn(1:10),localimagesize,1:100);
    movdirectory = fd;
    avios = mov(:,:,2:2:end);
    avios = median(avios,3);
    figure;imagesc(avios);colormap('gray');axis equal
    disp('select window mask')
    roi = drawpolygon;windowmask = createMask(roi);
end
bgdirectory = uigetdir(cd,'select background image directory');
[fn,fd] = uigetfile('*.mat','select metadata file');
metaname = [fd,fn];
savedirectory = fd;
[fn,fd] = uigetfile('*.mat','select background metadata file');
bgmetaname = [fd,fn];

savename = input('save results as: ',"s");

cd(savedirectory);
save('referencingInfo.mat','vesselmaskname','movdirectory','windowmask','bgdirectory','metaname',...
    'bgmetaname','savedirectory','savename','GcampSmoothSize','rootname','localimagesize',...
    'isReferenceSession','stimtype');


